#include "map.hpp"

Map::Map(MapView *mapview) : _mapview(mapview) {
	_hasChanged = false;
	connect(_mapview, SIGNAL(changed()), this, SLOT(changed()));
}

///// Methode Get
MapView* Map::getMapView() { return _mapview; }
QString Map::getPathBackground() { return _backgroundPath; }
QString Map::getPathMusic() { return _musicPath; }
QString Map::getName() { return _name; }
QString Map::getAuthor() { return _author; }
QString Map::getComment() { return _comment; }
bool Map::hasChanged() { return _hasChanged; }

///// Methode Set
void Map::setMapView(MapView *mapview) { _mapview = mapview; }
void Map::setComment(QString comment) { _comment = comment; }
void Map::setName(QString name) { _name = name; }
void Map::setAuthor(QString author) { _author = author; }
void Map::setPathBackground(QString path_background) { _backgroundPath = path_background; }
void Map::setPathMusic(QString path_music) { _musicPath = path_music; }
void Map::setChanged(bool hasChanged) { _hasChanged = hasChanged; }

///////////////////

bool Map::save() {
	QString path = QFileDialog::getSaveFileName(0, "Enregistrer votre fichier level", QString(), "Fichier Level (*.lvl)");
	if(path == NULL) return false;

	QFile file(path);

	if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
		return false;


	QTextStream out(&file);
	out << "Author = " << _author << endl;
	out << "Comment = " << _comment << endl;
	out << "Name = " << _name << endl;				// Nom de la map
	out << "Music = " << _musicPath << endl;		// Musique de la map
	out << "Fond = " << _backgroundPath << endl;	// Fond
	out << "Fond1 = " << endl << endl;				// 2e Fond

	//// Personnage ///
	out << "[character]" << endl;
	for(int i = 0; i < _mapview->getScene()->items().count(); i++)
		if(_mapview->getScene()->items().at(i) != _mapview->getFond())
		out << i << "x:" << _mapview->getScene()->items().at(i)->x() << " / y:" << _mapview->getScene()->items().at(i)->y() << "\n";
	out << "end" << endl << endl;

	// Carte /////
	out << "[map]" << endl;
	out << "end" << endl << endl;

	// Objet /////
	out << "[object]" << endl;
	out << "end" << endl << endl;

	// Monstre ///
	out << "[monster]" << endl;
	out << "end" << endl << endl;

	// Code //
	out << "[code]" << endl;
	out << "end" << endl;

	_hasChanged = false;
	return true;
}

void Map::changed() { _hasChanged = true; }

void Map::clear() {
	_author = "";
	_comment = "";
	_backgroundPath = "";
	_musicPath = "";
	_name = "";
	_hasChanged = false;
}

bool Map::seekWord(QString &line, QString word) {
   if(line.size() < word.size())
	   return false;

	for(int i = 0; i < word.size(); i++)
		if(line.at(i) != word.at(i))
			return false;

	 return true;
}

void Map::OpenMap() {
	QString path = QFileDialog::getOpenFileName(0, "Ouvrir un fichier level", QString(), "Fichier Level (*.lvl)");

	if(path == NULL)
		return;

	QFile file(path);

	if(!file.open(QIODevice::ReadOnly))
		return;

	QTextStream in(&file);
	QString line;

	do {
		line = in.readLine();

		if(seekWord(line, "Name ="))
			_name = line.section("= ", 1, 1);
		else if(seekWord(line, "Author ="))
			_author = line.section("= ", 1, 1);
		else if(seekWord(line, "Comment ="))
			_comment = line.section("= ", 1, 1);
	}while(!line.isNull());
}
